package com.person.title.service.impl;

import java.util.List;

import javax.servlet.http.HttpServletRequest;

import com.person.base.model.BaseDept;
import com.person.commons.utils.DAO;
import com.person.title.model.Title;
import com.person.title.model.TitleSub;
import com.person.title.service.SubFuncService;

public class SubFuncServiceImpl implements SubFuncService {

	
	private DAO dao;

	public DAO getDao() {
		return dao;
	}

	public void setDao(DAO dao) {
		this.dao = dao;
	}
	
	
	@Override
	public String subFuncTree(String parentCode, HttpServletRequest request) {
		
		String ctx = request.getContextPath() ;		
		StringBuffer responseXML = new StringBuffer ("<?xml version='1.0' encoding='UTF-8'?><tree></tree>") ;
			
		List <TitleSub> titleSubs = this.dao.find("from TitleSub where titleCodeGroup like '" + parentCode + "__' order by titleCodeGroup") ;
		if (titleSubs != null && titleSubs.size() > 0) {								
			responseXML = new StringBuffer("<?xml version='1.0' encoding='UTF-8'?><tree>") ;
			//循环一级节点
			for (TitleSub titleSub : titleSubs) {
				List <TitleSub> Subs = this.dao.find("from TitleSub where titleCodeGroup like '" + titleSub.getTitleCodeGroup() + "__'") ;
				
				responseXML.append("<tree") ;
				//节点名称
				responseXML.append(" text='" + titleSub.getSubName() + "'") ;
				//节点超链接
				responseXML.append(" src='" + ctx+ "/subFunc/subFunc_subFuncTree.action?parentCode=" + titleSub.getTitleCodeGroup() +"'") ;
				
				//判断叶子节点
				if (Subs == null || Subs.size() == 0) {
					//节点超链接
					responseXML.append(" action='" + ctx+ "/subFunc/subFunc_managerSubFunc.action?titleSub.subId=" + titleSub.getSubId() + "'") ;
				}
				
				//节点超链接目标
				responseXML.append(" target='mright'") ;
				
				
				if ((titleSub.getSubType()).equals("1")) {
					//会员栏目节点图片
					responseXML.append(" icon='" + ctx + "/images/main/depart.gif'") ;
					//会员栏目节点打开图片
					responseXML.append(" openIcon='" + ctx + "/images/main/depart.gif'") ;
					
				} else {
					//非会员栏目节点图片
					responseXML.append(" icon='" + ctx + "/images/main/depart.gif'") ;
					//非会员栏目节点打开图片
					responseXML.append(" openIcon='" + ctx + "/images/main/depart.gif'") ;
					
				}
				
				//节点
				responseXML.append("/>") ;
			}
			responseXML.append("</tree>") ;					
		}			
					
		return responseXML.toString() ;
	}

	
	
	
	@Override
	public void updateTitleSubFunc(String subId, String funcId) {
		String hql = "update TitleSub ts  set ts.funcId=? where ts.subId='"+subId+"'" ;
		this.dao.updateBulk(hql, funcId) ;
		
	}

	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
}
